package com.gec.wiki.controller;


import com.gec.wiki.po.Category;
import com.gec.wiki.po.req.CategoryQueryReq;
import com.gec.wiki.po.resp.CategoryQueryResp;
import com.gec.wiki.po.resp.CommonResp;
import com.gec.wiki.po.resp.PageResp;
import com.gec.wiki.service.ICategoryService;
import com.gec.wiki.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 林云
 * @since 2023-10-30
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/getCategoryListByPage")
    public CommonResp getCategoryListByPage(@Valid CategoryQueryReq req) {
        PageResp<CategoryQueryReq> repsPage = categoryService.getCategoryListByPage(req);
        CommonResp<PageResp<CategoryQueryReq>> resp = new CommonResp<>();
        resp.setContent(repsPage);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@RequestBody CategoryQueryReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        categoryService.saveOrUpdate(category);

        CommonResp resp = new CommonResp<>();
        return resp;
    }

    @GetMapping("/remove")
    public CommonResp remove(int id) {
        categoryService.removeById(id);
        CommonResp<Object> resp = new CommonResp<>();
        return resp;
    }

    @GetMapping("/allList")
    public CommonResp allList(CategoryQueryReq req){
        List<CategoryQueryResp> resp = categoryService.allList(req);
        CommonResp<List<CategoryQueryResp>> listCommonResp = new CommonResp<>();
        listCommonResp.setContent(resp);
        return listCommonResp;
    }
}
